package demo.engine.template.image;

import org.fit.cssbox.demo.ImageRenderer;
import org.fit.cssbox.layout.Dimension;
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class CssBoxDemo {

    public static void html2Image(String url, String path) throws IOException, SAXException {
        System.out.println("start");
        ImageRenderer render = new ImageRenderer();
        FileOutputStream out = new FileOutputStream(path);
        /*将url网页写入生成文件中*/
        render.renderURL(url, out);
        System.out.println("OK");
    }

    /**
     * 通过网格链接将网页转成图片
     *
     * @throws IOException
     * @throws SAXException
     */
    @Test
    public void cssboxTest() throws IOException, SAXException {
        //  网络链接的html
        String url = "https://developers.weixin.qq.com/miniprogram/dev/component/canvas.html";
        /*生成文件的路径*/
        String path = "/Users/xmly/Documents/tmp/html.png";

        html2Image(url, path);
    }

    /**
     * 将本地HTML文件转图片
     *
     * @throws Exception
     */
    @Test
    public void testLocalHtml2Image() throws Exception {
        // Load your Markdown content (replace with your own)
//        String markdownContent = "# Hello, world!\nThis is a *Markdown* example.";
        String htmlFilePath = "/Users/xmly/Documents/tmp/test.html";
//        MarkdownDemo.markdown2HtmlFile(markdownContent,htmlFilePath);
        String imagePath = "/Users/xmly/Documents/tmp/test.png";
        ImageRenderer render = new ImageRenderer();
        render.setWindowSize(new Dimension(800, 1000), false);

        System.out.println("kaishi");
        String url = new File(htmlFilePath).toURI().toString();
        FileOutputStream out = new FileOutputStream(imagePath);
        render.renderURL(url, out);
        System.out.println("OK");
    }
}
